﻿Ext.define('App.desktop.Icon', {
    extend: 'Ext.container.Container',
    alias: 'widget.DesktopIcon',
    requires: [
        'App.desktop.GridWindow',
        'App.desktop.Workspace'
    ],
    src: null,
    label: null,
    entity: null,

    listeners: {
        afterrender: function (me) {

            var el = me.el;

            el.on('click', function () {

                me.setFocus();
            });

            el.on('dblclick', function () {

                me.setFocus();
                App.desktop.Workspace.openGridWindow(me.window, {
                    entity: me.entity
                });
            });

            el.on('contextmenu', function (e) {

                e.preventDefault();

                var xy = [e.getPageX(), e.getPageY()];
                me.contextMenu.showAt(xy);
            });
        }
    },

    setFocus: function () {
        var me = this;
        var desktop = Ext.application.Desktop;

        if (me != desktop.selIcon) {

            if (desktop.selIcon) {
                desktop.selIcon.el.removeCls(desktop.config.highlightClass);
            }

            me.el.focus();
            me.el.addCls(desktop.config.highlightClass);
            desktop.selIcon = me;
        }
    },

    initComponent: function () {

        var me = this;
        me.typeId = me.id;
        me.id = 'icon' + me.id;

        me.contextMenu = new Ext.menu.Menu({
            items: [
                {
                    text: 'Open',
                    iconCls: 'open',
                    handler: function () {

                        App.desktop.Workspace.openGridWindow(me.window, {
                            entity: me.entity
                        });
                    }
                }
            ]
        });

        me.items = [
            {
                xtype: 'image',
                width: me.width,
                height: me.height,
                src: me.src
            },
            {
                xtype: 'label',
                text: me.label || me.entity.Name
            }
        ];

        me.callParent(arguments);
    }

});